﻿
'*******************************************************************
' Program: Data Center Visio Rack Tool
' Author: Albert E Edlund
' Date:
'
' Purpose:
'
'
'*******************************************************************


Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging

Imports System.Data
Imports System.Drawing
Imports System.IO
Imports System.Xml


Imports DC_Rack.Objects.clsDC_RackData


'
' This is where we manipulate 
'
Public Class clsTopoEdge

#Region "Properties and definitions"


    Public m_db As Database = Nothing
    Public m_ds As DataSet = Nothing
    Private m_ds_points As DataSet = Nothing
    Public m_dt As DataTable = Nothing

    Public m_oc As Collection(Of dc_TopoEdge)
    Public m_dc As dc_TopoEdge


    Public m_dt_xml As Xml.XmlDocument


    ' this is where we store the data that we want to put into a table
    Private m_colTempPath As Collection(Of dc_TopoEdge)

    Public m_deleteCommand As DbCommand
    Public m_insertCommand As DbCommand
    Public m_updateCommand As DbCommand

    Public Const readAll As String = "selecttblTopoEdge"


    Private m_colTopoPoints As Collection(Of dc_TopoPoint)
    Property _colTopoPoints() As Collection(Of dc_TopoPoint)
        Get
            If m_colTopoPoints Is Nothing Then
                m_colTopoPoints = New Collection(Of dc_TopoPoint)
            End If
            Return Me.m_colTopoPoints
        End Get
        Set(ByVal value As Collection(Of dc_TopoPoint))
            If m_colTopoPoints Is Nothing Then
                m_colTopoPoints = New Collection(Of dc_TopoPoint)
            End If
            Me.m_colTopoPoints = value
        End Set
    End Property

    Private m_colActivePoints As Collection(Of dc_TopoPoint)
    Property _colActivePoints() As Collection(Of dc_TopoPoint)
        Get
            If m_colActivePoints Is Nothing Then
                m_colActivePoints = New Collection(Of dc_TopoPoint)
            End If
            Return Me.m_colActivePoints
        End Get
        Set(ByVal value As Collection(Of dc_TopoPoint))
            If m_colActivePoints Is Nothing Then
                m_colActivePoints = New Collection(Of dc_TopoPoint)
            End If
            Me.m_colActivePoints = value
        End Set
    End Property

    Private m_colItemEdge As Collection(Of dc_TopoEdge)
    Property _colActiveEdges() As Collection(Of dc_TopoEdge)
        Get
            If m_colItemEdge Is Nothing Then
                m_colItemEdge = New Collection(Of dc_TopoEdge)
            End If
            Return Me.m_colItemEdge
        End Get
        Set(ByVal value As Collection(Of dc_TopoEdge))
            If m_colItemEdge Is Nothing Then
                m_colItemEdge = New Collection(Of dc_TopoEdge)
            End If
            Me.m_colItemEdge = value
        End Set
    End Property

    Private m_colNewPaths As Collection(Of dc_TopoEdge)
    Property _colNewPaths() As Collection(Of dc_TopoEdge)
        Get
            If m_colNewPaths Is Nothing Then
                m_colNewPaths = New Collection(Of dc_TopoEdge)
            End If
            Return Me.m_colNewPaths
        End Get
        Set(ByVal value As Collection(Of dc_TopoEdge))
            If m_colNewPaths Is Nothing Then
                m_colNewPaths = New Collection(Of dc_TopoEdge)
            End If
            Me.m_colNewPaths = value
        End Set
    End Property

    Private m_parentform As Object
    Property _ParentForm() As Object
        Get
            Return Me.m_parentform
        End Get
        Set(ByVal value As Object)
            Me.m_parentform = value
        End Set
    End Property

    '********* visio snippet vb.net vis0041vb ******************
    Private _drawingApplication As Microsoft.Office.Interop.Visio.Application
    Public Property drawingApplication() As Microsoft.Office.Interop.Visio.Application
        Get
            Return Me._drawingApplication
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Application)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("ApplicationValueIsNull", _
                    "drawingApplication is Null.")
            End If
            Me._drawingApplication = value
        End Set
    End Property

    '********* visio snippet vb.net vis0043vb ******************
    Private _drawingDocument As Microsoft.Office.Interop.Visio.Document
    Public Property drawingDocument() As Microsoft.Office.Interop.Visio.Document
        Get
            Return Me._drawingDocument
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Document)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("DocumentValueIsNull", _
                    "drawingDocument is Null.")
            End If
            Me._drawingDocument = value
        End Set
    End Property

    '********* visio snippet vb.net vis0045vb ******************
    Private _drawingWindow As Microsoft.Office.Interop.Visio.Window
    Public Property drawingWindow() As Microsoft.Office.Interop.Visio.Window
        Get
            Return Me._drawingWindow
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Window)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("WindowValueIsNull", _
                    "drawingWindow is Null.")
            End If
            Me._drawingWindow = value
        End Set
    End Property

    '********* visio snippet vb.net vis0044vb ******************
    Private _drawingPage As Microsoft.Office.Interop.Visio.Page
    Public Property drawingPage() As Microsoft.Office.Interop.Visio.Page
        Get
            Return Me._drawingPage
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Page)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("PageValueIsNull", _
                    "drawingPage is Null.")
            End If
            Me._drawingPage = value
        End Set
    End Property




#End Region

#Region " Move/Transform Data "

    '
    ' create columns
    '
    Public Sub CreateTableColumns(ByRef tempTable As DataTable)

        Dim newDC As dc_TopoEdge = New dc_TopoEdge

        Try

            Dim colTopoEdgePortAKey As New System.Data.DataColumn
            colTopoEdgePortAKey.ColumnName = "TopoEdgePortAKey"
            colTopoEdgePortAKey.DataType = GetType(Integer)
            colTopoEdgePortAKey.DefaultValue = newDC.constTopoEdgePortDataKey
            tempTable.Columns.Add(colTopoEdgePortAKey)

            Dim colTopoEdgePortBKey As New System.Data.DataColumn
            colTopoEdgePortBKey.ColumnName = "TopoEdgePortBKey"
            colTopoEdgePortBKey.DataType = GetType(Integer)
            colTopoEdgePortBKey.DefaultValue = newDC.constTopoEdgePortDataKey
            tempTable.Columns.Add(colTopoEdgePortBKey)


            Dim colTopoEdgeLinks As New System.Data.DataColumn
            colTopoEdgeLinks.ColumnName = "TopoEdgeLinks"
            colTopoEdgeLinks.DataType = GetType(String)
            colTopoEdgeLinks.DefaultValue = newDC.constTopoEdgeLinks
            tempTable.Columns.Add(colTopoEdgeLinks)

            Dim colTopoEdgeLinkCt As New System.Data.DataColumn
            colTopoEdgeLinkCt.ColumnName = "TopoEdgeLinkCt"
            colTopoEdgeLinkCt.DataType = GetType(Integer)
            colTopoEdgeLinkCt.DefaultValue = newDC.constTopoEdgeLinkCt
            tempTable.Columns.Add(colTopoEdgeLinkCt)

            Dim colTopoEdgeLink As New System.Data.DataColumn
            colTopoEdgeLink.ColumnName = "TopoEdgeLink"
            colTopoEdgeLink.DataType = GetType(Integer)
            colTopoEdgeLink.DefaultValue = newDC.constTopoEdgeLink
            tempTable.Columns.Add(colTopoEdgeLink)

            Dim colTopoEdgeBuildDate As New System.Data.DataColumn
            colTopoEdgeBuildDate.ColumnName = "TopoEdgeBuildDate"
            colTopoEdgeBuildDate.DataType = GetType(Date)
            colTopoEdgeBuildDate.DefaultValue = newDC.constTopoEdgeBuildDate
            tempTable.Columns.Add(colTopoEdgeBuildDate)

            newDC = Nothing

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        If newDC IsNot Nothing Then
            newDC = Nothing
        End If


    End Sub


    '
    ' use for writing records
    '
    Public Sub movePropertiesToDataRow _
            (ByRef tempRow As DataRow, _
             ByVal tempDC As dc_TopoEdge)

        Try

            With tempDC
                tempRow.SetField("TopoEdgePortAKey", .TopoEdgePortAKey)
                tempRow.SetField("TopoEdgePortBKey", .TopoEdgePortBKey)
                tempRow.SetField("TopoEdgeLinks", .TopoEdgeLinks)
                tempRow.SetField("TopoEdgeLinkCt", .TopoEdgeLinkCt)
                tempRow.SetField("TopoEdgeLink", .TopoEdgeLink)
                tempRow.SetField("TopoEdgeBuildDate", .TopoEdgeBuildDate)
            End With



        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
            (ByVal tempRow As DataRow)

        Try

            With m_dc
                .initProperties()
                .TopoEdgePortAKey = tempRow.Item("TopoEdgePortAKey")
                .TopoEdgePortBKey = tempRow.Item("TopoEdgePortBKey")
                .TopoEdgeLinks = tempRow.Item("TopoEdgeLinks")
                .TopoEdgeLinkCt = tempRow.Item("TopoEdgeLinkCt")
                .TopoEdgeLinkCt = tempRow.Item("TopoEdgeLink")
                .TopoEdgeBuildDate = tempRow.Item("TopoEdgeBuildDate")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
            (ByVal tempRow As DataRow, _
             ByRef tempDC As dc_TopoEdge)

        Try

            With tempDC
                .initProperties()
                .TopoEdgePortAKey = tempRow.Item("TopoEdgePortAKey")
                .TopoEdgePortBKey = tempRow.Item("TopoEdgePortBKey")
                .TopoEdgeLinks = tempRow.Item("TopoEdgeLinks")
                .TopoEdgeLinkCt = tempRow.Item("TopoEdgeLinkCt")
                .TopoEdgeLinkCt = tempRow.Item("TopoEdgeLink")
                .TopoEdgeBuildDate = tempRow.Item("TopoEdgeBuildDate")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' for moving active table to collection for wpf
    '
    Public Sub moveDataTableToObservableCollection _
            (ByVal dtTemp As DataTable)

        Try

            If 0 < dtTemp.Rows.Count Then

                If m_oc IsNot Nothing Then
                    m_oc = Nothing
                    m_oc = New Collection(Of dc_TopoEdge)
                Else
                    m_oc = New Collection(Of dc_TopoEdge)
                End If

                Dim drTemp As DataRow
                Dim dcTemp As dc_TopoEdge
                For Each drTemp In dtTemp.Rows
                    dcTemp = New dc_TopoEdge
                    moveDataRowToProperties(drTemp, dcTemp)
                    m_oc.Add(dcTemp)
                Next

            End If

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Function moveDataTableToXML() As XmlDocument

        Dim writer = New System.IO.StringWriter
        Dim m_dt_xml As Xml.XmlDocument = Nothing

        Try


            m_dt.WriteXml(writer, XmlWriteMode.WriteSchema, True)
            m_dt_xml = Nothing
            m_dt_xml = New Xml.XmlDocument
            m_dt_xml.LoadXml(writer.ToString)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally

            writer = Nothing

        End Try

        Return m_dt_xml


    End Function




#End Region

#Region " Standard commands "

    '
    '
    '
    Private Sub buildInsertItemEdgeCommand()
        Try


            m_insertCommand = m_db.GetStoredProcCommand("insertItemEdge")
            Dim cmdActive As DbCommand = m_insertCommand

            m_db.AddInParameter(cmdActive, "itemedgePortAKey", DbType.Int32, "itemedgePortAKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "itemedgePortBKey", DbType.Int32, "itemedgePortBKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "itemedgeLinks", DbType.String, "itemedgeLinks", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "itemedgeLinkCt", DbType.Int16, "itemedgeLinkCt", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "itemedgeLinkKey", DbType.Int32, "itemedgeLinkKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "itemedgeBuildDate", DbType.Date, "itemedgeBuildDate", DataRowVersion.Current)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub

    '
    '
    '
    Private Sub buildUpdateItemEdgeCommand()
        Try

            m_updateCommand = m_db.GetStoredProcCommand("updateItemEdge")
            Dim cmdActive As DbCommand = m_updateCommand

            m_db.AddInParameter(cmdActive, "itemedgePortAKey", DbType.Int32, "itemedgePortAKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "itemedgePortBKey", DbType.Int32, "itemedgePortBKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "itemedgeLinks", DbType.String, "itemedgeLinks", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "itemedgeLinkCt", DbType.Int16, "itemedgeLinkCt", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "itemedgeLinkKey", DbType.Int32, "itemedgeLinkKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "itemedgeBuildDate", DbType.Date, "itemedgeBuildDate", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildDeleteItemEdgeCommand()
        Try


            m_deleteCommand = m_db.GetStoredProcCommand("deleteItemEdge")
            Dim cmdActive As DbCommand = m_deleteCommand

            m_db.AddInParameter(cmdActive, "itemedgePortAKey", DbType.Int32, "itemedgePortAKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "itemedgePortBKey", DbType.Int32, "itemedgePortBKey", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub

    '
    '
    '
    Public Function dbUpdateDataSet() As Int16

        Dim rowsAffected As Int16 = 0

        Try

            rowsAffected = m_db.UpdateDataSet(m_ds, DC_RackTables.tblTopoEdge.ToString, _
                                        m_insertCommand, _
                                        m_updateCommand, _
                                        m_deleteCommand, _
                                        UpdateBehavior.Standard)
            'msgbox rowsAffected

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return rowsAffected

    End Function


    '
    ' create a record based on property values move it to the generic datatable
    ' then copy the table to the database
    '
    Public Function addTopoEdgeSourceRecord _
            (ByVal tempDC As dc_TopoEdge, _
             Optional ByVal blnSilent As Boolean = False) _
             As Integer

        Dim intRowsAffected As Integer = 0

        Try

            initDataSetAndTable()

            Dim m_dr As DataRow = m_ds.Tables(DC_RackTables.tblTopoEdge.ToString).NewRow
            movePropertiesToDataRow(m_dr, tempDC)
            m_ds.Tables(DC_RackTables.tblTopoEdge.ToString).Rows.Add(m_dr)
            intRowsAffected = dbUpdateDataSet()

            If blnSilent = False Then
                MsgBox("DataRowsUpated = " & intRowsAffected)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return intRowsAffected

    End Function

    '
    ' clear the general dataset and table, create columns in the table, and move it to
    ' the dataset
    '
    Public Sub initDataSetAndTable()

        Try

            ' make sure the root stuff is there
            If m_ds Is Nothing Then
                m_ds = New DataSet
            Else
                m_ds = Nothing
                m_ds = New DataSet
            End If
            m_ds.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema

            If m_dt Is Nothing Then
                m_dt = New DataTable
            Else
                m_dt = Nothing
                m_dt = New DataTable
            End If

            If m_oc Is Nothing Then
                m_oc = New Collection(Of dc_TopoEdge)
            Else
                m_oc = Nothing
                m_oc = New Collection(Of dc_TopoEdge)
            End If

            If m_dc Is Nothing Then
                m_dc = New dc_TopoEdge
            Else
                m_dc = Nothing
                m_dc = New dc_TopoEdge
            End If

            CreateTableColumns(m_dt)
            m_dt.TableName = DC_RackTables.tblTopoEdge.ToString

            m_ds.Tables.Add(m_dt)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub




#End Region

#Region "Discovery"


    '
    ' go get a list of item points that are on active nodes
    ' and save them locally so we don't have to search again
    '
    Public Sub preloadPointData()

        ' create a TopoPoint module
        Dim clsPoints As clsTopoPoint
        Try

            'initialize it
            clsPoints = New clsTopoPoint
            ' load the list of all TopoPoints
            clsPoints.dbSelectTopoPoints()
            ' load the list of just the active points
            clsPoints.dbSelectTopoPointsActivePoints()

            _colActivePoints = clsPoints._colActivePoints
            _colTopoPoints = clsPoints._colTopoPoints

            'Debug.Print(_colActivePoints.count & " " & _colTopoPoints.count)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If

        Finally
            ' close the module
            clsPoints = Nothing
        End Try

    End Sub

    '
    ' this compares the TopoPoint table to the network links table to build the
    ' edge table with the existing links
    '
    '
    Public Sub dbInittopoEdgeTableData()


        Dim clsLinks As DC_Rack.Objects.clsNetworkLinks = New DC_Rack.Objects.clsNetworkLinks(drawingApplication, drawingDocument)


        Dim cmdReadLinks As DbCommand
        cmdReadLinks = m_db.GetStoredProcCommand("selecttblNetworkLink")
        Dim m_ds_links As DataSet = New DataSet
        Dim m_dt_Links As DataTable = New DataTable
        'Dim itemEdge As dc_TopoEdge
        'Dim m_dr As DataRow


        Try
            ' clear the database table
            dbDeleteItemEdges()



            'read the empty table into the class dataset
            'dbSelectItemEdges()
            'read the network links into the temp dataset 
            m_db.LoadDataSet(cmdReadLinks, m_ds_links, dc_racktables.tblNetworkLink.tostring)
            m_dt_Links = m_ds_links.Tables(dc_racktables.tblNetworkLink.tostring)

            'Dim addedRow As DataRow
            Dim colTemp As Collection = New Collection
            Dim intCt As Integer = 0

            If Not (_ParentForm Is Nothing) Then
                _ParentForm.lblOperation.Text = "Initializing Item Edges"
            End If

            'For Each m_dr In m_dt_Links.Rows
            '    intCt += 1

            '    If Not (_ParentForm Is Nothing) Then
            '        _ParentForm.lblStatus.Text = "Item Edge Row " & intCt & " of " & m_dt_Links.Rows.Count
            '    End If

            '    itemEdge = Nothing
            '    itemEdge = New dc_topoedge

            '    pItemEdgePointAKey = m_dr.Item("linkNodeAKey")
            '    pItemEdgePointAPort = m_dr.Item("linkPortA")
            '    pItemEdgePointBKey = m_dr.Item("linkNodeBKey")
            '    pItemEdgePointBPort = m_dr.Item("linkPortB")
            '    pItemEdgeLinkKey = m_dr.Item("linkKey")
            '    colTemp = Nothing
            '    colTemp = New Collection
            '    colTemp.Add(pItemEdgeLinkKey)
            '    pItemEdgeLinks = colTemp
            '    pItemEdgeLinksStr = "[" & pItemEdgeLinkKey & "]"
            '    pItemEdgeLinksCt = colTemp.Count
            '    pItemEdgeBuildDate = Now()

            '    itemEdge.m_pointakey = pItemEdgePointAKey
            '    itemEdge.m_pointaport = pItemEdgePointAPort
            '    itemEdge.m_pointbkey = pItemEdgePointBKey
            '    itemEdge.m_pointbport = pItemEdgePointBPort
            '    itemEdge.m_links = pItemEdgeLinks
            '    itemEdge.m_linkct = pItemEdgeLinksCt
            '    itemEdge.m_lnglinkkey = pItemEdgeLinkKey

            '    If _colActiveEdges.ExistsLink(itemEdge) = False Then

            '        ' save it to the collection
            '        _colActiveEdges.Add(itemEdge)

            '        ' save it to the dataset so we can put it in the database later
            '        addedRow = m_ds.Tables(topoEdgeTable).Rows.Add(New Object() _
            '                    {pItemEdgePointAKey, _
            '                    pItemEdgePointAPort, _
            '                    pItemEdgePointBKey, _
            '                    pItemEdgePointBPort, _
            '                    pItemEdgeLinksStr, _
            '                    pItemEdgeLinksCt, _
            '                    pItemEdgeLinkKey, _
            '                    Now()})

            '    Else

            '        ' if we find that a link has already been defined flag it
            '        _ParentForm.lblItemEdgesVerify.Text = "Failed"
            '        _ParentForm.lblItemEdgesVerify.ForeColor = System.Drawing.Color.Red
            '        Dim strError As String = "duplicate path - link index " & pItemEdgeLinkKey
            '        _ParentForm.logProductError(9999, "links", strError)

            '    End If

            'Next m_dr

            ' Debug.Print(m_ds.Tables(topoEdgeTable).Rows.Count & " " & _colActiveEdges.count)


        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally
            m_ds_links = Nothing
            m_dt_Links = Nothing
            cmdReadLinks = Nothing
        End Try





    End Sub

    Public Sub buildNewPaths()

        Try

            Dim clsPoint As clsTopoPoint
            clsPoint = New clsTopoPoint

            Dim clsNewEdge As dc_TopoEdge
            clsNewEdge = New dc_TopoEdge

            ' cannot start without a collection with an active set
            If Me._colActivePoints Is Nothing _
                Or Me._colActivePoints.Count < 1 Then
                Exit Sub
            End If

            If Me._colTopoPoints Is Nothing _
                Or Me._colTopoPoints.Count < 1 Then
                Exit Sub
            End If

            'Dim intX As Integer
            Dim intCt As Integer
            intCt = _colActivePoints.Count
            'Dim strMessage As String
            '            For intX = 0 To intCt - 1
            '                clsPoint = Nothing
            '                clsPoint = New dc_topopoint
            '                clsPoint = _colActivePoints.GetItem(intX)

            '                strMessage = "test path " & clsPoint.m_nodename & " / " & clsPoint.m_portname & " (" _
            '                       & CStr(intX) & " of " & CStr(intCt) & ")"
            '                _ParentForm.lblStatus.Text = strMessage
            '                System.Windows.Forms.Application.DoEvents()

            '                clsNewEdge = Nothing
            '                clsNewEdge = New dc_topoedge
            '                clsNewEdge = BuildPath(clsPoint)

            '                ' keep this for reporting
            '                If _colNewPaths.ExistsLink(clsNewEdge) = False Then
            '                    _colNewPaths.Add(clsNewEdge)
            '                End If

            '                'update this one to save to database
            '                If _colActiveEdges.ExistsLink(clsNewEdge) = False Then
            '                    _colActiveEdges.Add(clsNewEdge)
            '                End If

            'NextHandler:
            '            Next intX



        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If

        Finally

            _ParentForm.lblNewPathCount.Text = _colNewPaths.Count
            _ParentForm.lblItemEdgeCount.Text = _colActiveEdges.Count

        End Try


    End Sub

    '
    ' pass in an item point return an edge
    '
    Public Function BuildPath _
        (ByVal myPort As dc_TopoPoint) _
        As dc_TopoEdge

        Dim edgeReturn As dc_TopoEdge = New dc_TopoEdge

        Try

            'If myPort Is Nothing Then
            '    Debug.Print("Build Path received empty port")
            '    Return edgeReturn
            'End If

            'Dim blnReturn As Boolean
            'blnReturn = _colTopoPoints.ExistsPort(myPort)
            'If blnReturn = False Then
            '    Debug.Print("BuildPath Node=" & myPort.m_nodename & " / Port=" & myPort.m_portname & " not found")
            '    Return edgeReturn
            'End If

            '' initialize the temp path collection (clsColTopology)
            'If m_colTempPath Is Nothing Then
            '    m_colTempPath = New Collection(Of dc_TopoEdge)
            'Else
            '    m_colTempPath = Nothing
            '    m_colTempPath = New Collection(Of dc_TopoEdge)
            'End If

            '' get the link collection from the port object
            'Dim colWork As Collection
            'colWork = New Collection
            'colWork = myPort.m_links
            'If colWork.Count <> 1 Then
            '    Debug.Print("BuildPath Port wrong connection count (" & colWork.Count & ")")
            '    Return edgeReturn
            'Else
            '    ' passed the validation now go build the m_coltemppath
            '    tpGetNextPort(myPort)
            'End If

            '' return the list of paths
            'If m_colTempPath.Count <> 0 Then
            '    edgeReturn = getEndNodes()
            '    BuildPath = edgeReturn
            'Else
            '    Debug.Print("no path col " & myPort.m_nodename & " / " & myPort.m_portname)
            'End If

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If

        Finally
            BuildPath = edgeReturn

        End Try

    End Function

    '
    ' pass in the current port to determine the next port
    '
    Private Sub tpGetNextPort _
            (ByVal portCurrent As dc_TopoPoint)

        'Dim intX As Integer
        'Dim intLinkCount As Integer
        'Dim intCurrPort As Integer
        ' to work with edges
        Dim clsLink As dc_TopoEdge = New dc_TopoEdge
        Dim colNewLinks As Collection = New Collection
        colNewLinks = portCurrent._colLinks

        Dim portReturn As dc_TopoPoint = New dc_TopoPoint

        Dim portWorkA As dc_TopoPoint = New dc_TopoPoint
        Dim intWorkPortA As Integer
        intWorkPortA = -1

        Dim portWorkB As dc_TopoPoint = New dc_TopoPoint
        Dim intWorkPortB As Integer
        intWorkPortB = -1

        'Dim lngLinkKey As Int32
        'Dim lngRef As Int32
        'Dim strError As String

        Try

            '            ' make sure it's a real port
            '            intCurrPort = m_colTopoPoints.IndexOfPort(portCurrent)
            '            If intCurrPort = -1 Then
            '                Exit Sub
            '            End If

            '            intLinkCount = portCurrent.m_links.Count

            '            For intX = 1 To intLinkCount
            '                strError = ""

            '                ' get the linkid:
            '                ' in db mode this is the linkkey in the networklinks table
            '                lngLinkKey = CLng(portCurrent.m_links.Item(intX))

            '                lngRef = m_colItemEdge.IndexOfLinkKey(lngLinkKey)

            '                If lngRef <> -1 Then
            '                    clsLink = m_colItemEdge.GetItem(lngRef)

            '                    '
            '                    ' if the link is not there find the data and call the next one
            '                    '
            '                    If m_colTempPath.ExistsLink(clsLink) = False Then
            '                        m_colTempPath.Add(clsLink)
            '                        'Debug.Print "save link to path "

            '                        portWorkA = Nothing
            '                        portWorkA = New dc_topopoint
            '                        portWorkB = Nothing
            '                        portWorkB = New dc_topopoint
            '                        System.Windows.Forms.Application.DoEvents()

            '                        ' get the a side information
            '                        With portWorkA
            '                            .m_nodeindex = CLng(clsLink.m_pointakey)
            '                            .m_portname = clsLink.m_pointaport
            '                        End With
            '                        If m_colTopoPoints.ExistsPort(portWorkA) = True Then
            '                            intWorkPortA = m_colTopoPoints.IndexOfPort(portWorkA)
            '                            portWorkA = m_colTopoPoints.GetItem(intWorkPortA)
            '                        Else
            '                            strError = "itemPtA NF " & lngLinkKey & " " & portWorkA.m_nodeindex & " " & portWorkA.m_portname
            '                            Debug.Print(strError)
            '                            GoTo NextHandler
            '                        End If


            '                        ' get the b side information
            '                        With portWorkB
            '                            .m_nodeindex = CLng(clsLink.m_pointbkey)
            '                            .m_portname = clsLink.m_pointbport
            '                        End With
            '                        If m_colTopoPoints.ExistsPort(portWorkB) = True Then
            '                            intWorkPortB = m_colTopoPoints.IndexOfPort(portWorkB)
            '                            portWorkB = m_colTopoPoints.GetItem(intWorkPortB)
            '                        Else
            '                            strError = "itemPtB NF " & lngLinkKey & " " & portWorkB.m_nodeindex & " " & portWorkB.m_portname
            '                            Debug.Print(strError)
            '                            GoTo NextHandler
            '                        End If

            '                        ' decide which of the two ports to save
            '                        If (portWorkA.m_nodeindex = portCurrent.m_nodeindex) _
            '                            And (LCase(portWorkA.m_portname) = LCase(portCurrent.m_portname)) Then
            '                            portReturn = portWorkB
            '                            colNewLinks = portWorkB.m_links
            '                        End If
            '                        If (portWorkB.m_nodeindex = portCurrent.m_nodeindex) _
            '                            And (LCase(portWorkB.m_portname) = LCase(portCurrent.m_portname)) Then
            '                            portReturn = portWorkA
            '                            colNewLinks = portWorkA.m_links
            '                        End If

            '                        ' now chase it
            '                        'Debug.Print "catch  " & clsLink.m_lnglinkkey & " " & portCurrent.m_nodename & " " & portReturn.m_nodename
            '                        tpGetNextPort(portReturn)

            '                    Else
            '                        'Debug.Print "already found that one"
            '                    End If
            '                Else
            '                    ' could not find the link key 
            '                End If

            'NextHandler:
            '            Next intX

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If

        End Try
    End Sub

    '
    ' this is the logic for chasing a path in the collection
    ' of path edge (path) items
    '
    Private Function getEndNodes _
            (Optional ByVal blnDbCall As Boolean = False) _
            As dc_TopoEdge

        Dim retPath As dc_TopoEdge = New dc_TopoEdge

        On Error GoTo ErrHandler

        'Dim intX As Integer

        'Dim edgeTest As dc_TopoEdge



        'Dim pointA As dc_TopoPoint
        'pointA = New dc_TopoPoint
        'Dim lngPointA As Int32

        'Dim PointB As dc_TopoPoint
        'PointB = New dc_TopoPoint
        'Dim lngPointB As Int32

        '' define a temporary collection set
        'Dim colTempEndPoints As dc_TopoPoint = New dc_TopoPoint

        'Dim colPathIndices As Collection = New Collection

        ''
        '' a path can only have two active nodes (the end-points)
        ''
        'For intX = 0 To m_colTempPath.Count - 1
        '    edgeTest = Nothing
        '    edgeTest = New dc_TopoEdge
        '    edgeTest = m_colTempPath.Item(intX)
        '    pointA = Nothing
        '    pointA = New dc_TopoPoint
        '    PointB = Nothing
        '    PointB = New dc_TopoPoint

        '    colPathIndices.Add(edgeTest._colLinks.Item(1))


        '    With pointA
        '        pointA.m_nodeindex = edgeTest.m_pointakey
        '        pointA.m_portname = edgeTest.m_pointaport
        '    End With

        '    If _colTopoPoints.ExistsPort(pointA) Then
        '        lngPointA = _colTopoPoints.IndexOfPort(pointA)
        '        pointA = _colTopoPoints.GetItem(lngPointA)
        '        ' if this point is an active resource then save it
        '        If pointA.m_vnettype = 10 Then
        '            If colTempEndPoints.ExistsPort(pointA) = False Then
        '                colTempEndPoints.Add(pointA)
        '            End If
        '        End If
        '    End If

        '    With PointB
        '        PointB.m_nodeindex = edgeTest.m_pointbkey
        '        PointB.m_portname = edgeTest.m_pointbport
        '    End With
        '    If _colTopoPoints.ExistsPort(PointB) Then
        '        lngPointB = _colTopoPoints.IndexOfPort(PointB)
        '        PointB = _colTopoPoints.GetItem(lngPointB)
        '        ' if this point is an active resource then save it
        '        If PointB.m_vnettype = 10 Then
        '            If colTempEndPoints.ExistsPort(PointB) = False Then
        '                colTempEndPoints.Add(PointB)
        '            End If
        '        End If
        '    End If

        '    'Debug.Print "TestEP " & edgeTest.m_lnglinkkey & pointA.m_nodename & " " & PointB.m_nodename

        'Next intX


        'Dim intPoints As Integer
        'intPoints = colTempEndPoints.count
        'If intPoints = 0 Then GoTo ExitHandler

        'Select Case intPoints
        '    Case 2
        '        pointA = colTempEndPoints.GetItem(0)
        '        PointB = colTempEndPoints.GetItem(1)
        '        retPath.m_pointakey = pointA.m_nodeindex
        '        retPath.m_pointaport = pointA.m_portname
        '        retPath.m_pointbkey = PointB.m_nodeindex
        '        retPath.m_pointbport = PointB.m_portname
        '        retPath.m_lnglinkkey = -1
        '        retPath.m_links = colPathIndices
        '    Case 1
        '        With retPath
        '            pointA = colTempEndPoints.GetItem(0)
        '            retPath.m_pointakey = pointA.m_nodeindex
        '            retPath.m_pointaport = pointA.m_portname
        '            retPath.m_pointbkey = -1
        '            retPath.m_pointbport = "na"
        '            retPath.m_lnglinkkey = -1
        '            retPath.m_links = colPathIndices
        '        End With
        '    Case Else
        '        '
        '        ' this usually turns out to be too many connections on a point
        '        '
        '        Debug.Print("End Node Failure (endpoints " & colTempEndPoints.count & ")")
        'End Select

        'retPath.m_links = colPathIndices
        'retPath.m_linkct = colPathIndices.Count

ExitHandler:
        getEndNodes = retPath
        Exit Function

ErrHandler:

        Debug.Print("getendnodes " & Err.Description)
        getEndNodes = retPath

    End Function


    Public Sub insertItemEdgeCollection()


        Dim intX As Integer
        Dim intCt As Integer
        intCt = -1
        Dim itemEdge As dc_TopoEdge = New dc_TopoEdge



        initDataSetAndTable()

        Try

            _ParentForm.lblOperation.Text = "Saving Edge Information To Database"
            _ParentForm.lblStatus.Text = "prep datatable"

            dbDeleteItemEdges()
            Dim cmd As DbCommand
            cmd = m_db.GetStoredProcCommand(readAll)
            m_db.LoadDataSet(cmd, m_ds, DC_RackTables.tblTopoEdge.ToString)

            m_dt = m_ds.Tables(DC_RackTables.tblTopoEdge.ToString)

            If _colActiveEdges.Count < 1 Then
                MsgBox("colItemEdges is Empty")
                ' nothing to save to the database 
                Exit Sub
            Else
                _ParentForm.lblStatus.Text = "Moving Links to table"
                Dim newRow As DataRow
                For intX = 0 To _colActiveEdges.Count - 1
                    m_dc = _colActiveEdges.Item(intX)
                    If Not (m_dc Is Nothing) Then
                        newRow = m_dt.NewRow
                        movePropertiesToDataRow(newRow, m_dc)
                        m_dt.Rows.Add(newRow)
                    End If
                Next intX

            End If



            intCt = m_dt.Rows.Count
            Debug.Print(intCt)

            _ParentForm.lblStatus.Text = "Moving table to database"


            Dim rowsAffected As Integer = m_db.UpdateDataSet(m_ds, DC_RackTables.tblTopoEdge.ToString, _
                                    m_insertCommand, _
                                    m_updateCommand, _
                                    m_deleteCommand, _
                                    UpdateBehavior.Standard)
            'msgbox rowsAffected



        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try




    End Sub


#End Region

    '
    '
    '
    Public Sub dbSelectTopoEdges()


        Try


            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selecttblTopoEdge")
            m_db.LoadDataSet(cmd, m_ds, DC_RackTables.tblTopoEdge.ToString)

            m_dt = m_ds.Tables(DC_RackTables.tblTopoEdge.ToString)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub

    '
    '
    '
    Public Sub dbSelectItemEdgeByTopoPoint _
                (ByVal intNodeIndex As Int32, _
                 ByVal strPortName As String)

        Try


            initDataSetAndTable()


            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectItemEdgeByTopoPoint")
            m_db.AddInParameter(cmd, "@NodeIndex", DbType.Int32, intNodeIndex)
            m_db.AddInParameter(cmd, "@PortName", DbType.String, LCase(strPortName))
            m_db.LoadDataSet(cmd, m_ds, DC_RackTables.tblTopoEdge.ToString)

            m_dt = m_ds.Tables(DC_RackTables.tblTopoEdge.ToString)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub

    '
    '
    '
    Public Sub dbDeleteItemEdges()

        Try

            Dim cmd As DbCommand = m_db.GetStoredProcCommand("deletetblTopoEdge")
            Dim deletedCount As Integer = m_db.ExecuteNonQuery(cmd)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


#Region " constructor "

    Public Sub New()


        m_db = DatabaseFactory.CreateDatabase()

        initDataSetAndTable()

        m_colItemEdge = New Collection(Of dc_TopoEdge)
        m_colActivePoints = New Collection(Of dc_TopoPoint)
        m_colTopoPoints = New Collection(Of dc_TopoPoint)

        buildInsertItemEdgeCommand()
        buildUpdateItemEdgeCommand()
        buildDeleteItemEdgeCommand()

    End Sub

#End Region



End Class
